package top.yangbuyi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.Menu;
import top.yangbuyi.domain.SimpleUser;
import top.yangbuyi.domain.User;
import top.yangbuyi.dto.MenuDto;
import top.yangbuyi.mapper.MenuMapper;
import top.yangbuyi.mapper.RoleMapper;
import top.yangbuyi.service.impl.MenuService;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/8/27
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
public class MenuServiceImpl implements MenuService {

	  @Autowired
	  private MenuMapper menuMapper;

	  @Autowired
	  private RoleMapper roleMapper;

	  /**
	   * @param menuDto
	   * @功能描述:查询全部菜单数据
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/3 16:42
	   */
	  @Override
	  public List<Menu> listAllMenus(MenuDto menuDto) {
			QueryWrapper<Menu> qw = new QueryWrapper<>();
			qw.like(StringUtils.isNotBlank(menuDto.getMenuName()), Menu.COL_MENU_NAME, menuDto.getMenuName());
			qw.like(StringUtils.isNotBlank(menuDto.getStatus()), Menu.COL_STATUS, menuDto.getStatus());
			return this.menuMapper.selectList(qw);
	  }


	  /**
	   * 查询菜单信息
	   * 如查用户是超级管理员，那么查询所有菜单和权限
	   * 如果用户是普通用户，那么根据用户ID关联角色和权限
	   *
	   * @param isAdmin    true  超级管理员  | false 普通管理员
	   * @param simpleUser 封装用户id 用户 名称
	   * @return menu
	   */
	  @Override
	  public List<Menu> selectMenuTree(boolean isAdmin, SimpleUser simpleUser) {
			QueryWrapper<Menu> menu = new QueryWrapper<>();
			menu.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
			menu.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_M, Constants.MENU_TYPE_C);
			menu.orderByAsc(Menu.COL_PARENT_ID);
			if (isAdmin) {
				  return menuMapper.selectList(menu);
			} else {
				  // 根据用户ID查询用有的菜单
				  return menuMapper.selectMenuListByUserId(simpleUser.getUserId());
			}
	  }


	  @Override
	  public Menu getMenuById(Integer id) {
			return this.menuMapper.selectById(id);
	  }

	  @Override
	  public int addMenu(MenuDto menuDto) {
			Menu menu = new Menu();
			BeanUtil.copyProperties(menuDto, menu);
			menu.setCreateBy(menuDto.getSimpleUser().getUserName());
			menu.setCreateTime(DateUtil.date());
			return this.menuMapper.insert(menu);
	  }

	  @Override
	  public int deleteMenuById(Long asList) {
			// 1.删除与之对应的菜单角色
			this.roleMapper.deleteRoleMenuByMenuIds(Arrays.asList(asList));
			// 2.删除菜单
			return this.menuMapper.deleteById(asList);
	  }


	  @Override
	  public int update(MenuDto menuDto) {
			Menu menu = new Menu();
			BeanUtil.copyProperties(menuDto, menu);
			menu.setUpdateBy(menuDto.getSimpleUser().getUserName());
			menu.setUpdateTime(DateUtil.date());
			return this.menuMapper.updateById(menu);
	  }

	  @Override
	  public boolean hasChilenByMenuID(Long menuid) {
			Long count = this.menuMapper.queryMenuByIDForChilen(menuid);
			return count > 0L ? true : false;
	  }

	  @Override
	  public List<Long> getMenuIdsByRoleId(Long roleId) {
			return this.menuMapper.queryMenuIdsByRoleId(roleId);
	  }

	  /*********************************************动态路由菜单********************************************/
	  @Override
	  public List<Map<String, Object>> dynamicMenu() {
			List<Map<String, Object>> list = new ArrayList<>();
			QueryWrapper<Menu> qw = new QueryWrapper<>();
			qw.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
			qw.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_M, Constants.MENU_TYPE_C);
			qw.orderByAsc(Menu.COL_PARENT_ID);
			List<Menu> sysMenus = menuMapper.selectList(qw);
			for (Menu sysMenu : sysMenus) {
				  //拼接父菜单
				  if (sysMenu.getParentId() == 0) {
						Map<String, Object> map = new HashMap<>();
						map.put("path", sysMenu.getPath());
						map.put("component", "Layout");
						map.put("redirect", "noRedirect");
						map.put("alwaysShow", true);
						map.put("name", sysMenu.getPath());
						//拼接图标
						Map<String, Object> icon = new HashMap<>();
						icon.put("title", sysMenu.getMenuName());
						icon.put("icon", "lock");
						map.put("meta", icon);
						//查询出父菜单的儿子菜单
						List<Map<String, Object>> chiledList = new ArrayList<>();
						List<Menu> menuByParentId = this.getMenuByParentId(sysMenu.getMenuId());
						//组装开始
						for (Menu menu : menuByParentId) {
							  Map<String, Object> childMap = new HashMap<>();
							  childMap.put("path", menu.getPath());
							  childMap.put("component", menu.getPath());
							  childMap.put("name", menu.getPath());
							  Map<String, Object> childIcon = new HashMap<>();
							  //儿子图标
							  childIcon.put("title", menu.getMenuName());
							  childIcon.put("icon", "lock");
							  childMap.put("meta", childIcon);
							  chiledList.add(childMap);

						}
						map.put("children", chiledList);
						list.add(map);
				  }

			}
			return list;
	  }


	  public List<Menu> getMenuByParentId(Long id) {
			QueryWrapper<Menu> qw = new QueryWrapper<>();
			qw.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
			qw.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_M, Constants.MENU_TYPE_C);
			qw.eq(Menu.COL_PARENT_ID, id);
			return menuMapper.selectList(qw);
	  }

}
